<?php

class tWebsetup_Layout extends tStandard_TreeItem
{	
	
	function view(cView $view) 
	{
		//prevent recursive use
		if ($this->cms->layout->working) return;
		$this->working = true;
		
		$output = parent::view($view);
		$output = str_replace("<?xml version=\"1.0\"?>","",$output);

		$dir = dirname(__FILE__)."/pretty_source/";
		
		require_once($dir."pretty_source/ps-config.php");
		require_once($dir."pretty_source/ps.php");
		require_once($dir."pretty_source/output-setup/html.php");

		$parser = new psParser(PS_MODE_XML, $ps_tags_indent_before, $ps_tags_indent_after, $ps_tags_break, $ps_tags_inline, $ps_tags_preformat, $ps_tags_single, $ps_attributes_order);

		//P&&S requires spaces
		$output = preg_replace("/<(\w+)\/>/","<\\1 />",$output);

		$output = $parser->parse($output);

		return $output;
	}

	function setSXML(SimpleXMLElement $inSXML, cView $view, SimpleXMLElement $outSXML)
	{
		if ($view->action=="show" && $view->doctype!="raw" && $view->id!=$this->id && $view->mode=="frontend") 
		{
			$node = $this->cms->tree->getNodeById($view->id);
			$inSXML->websetup_layout->main = (str_replace("<?xml version=\"1.0\"?>","",$s = $node->view($view)));
			
		}

		parent::setSXML($inSXML, $view, $outSXML);

		if ($view->action=="show" && $view->doctype!="raw" && $view->id!=$this->id && $view->mode=="frontend") 
		{
			foreach ($node->data->standard_treeitem->children() as $info) {
				$outSXML->websetup_layout->page->addChild($info->getName(),(string)$info);
			}
		}

		$this->cms->debugger->log(print_r($outSXML,true), $this);
		$this->cms->debugger->log(print_r($inSXML,true), $this);
		
		return $outSXML;
	}
	
}


?>